package com.achievement.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.PaperAuthorDTO;
import com.achievement.domain.entity.PaperAuthorEntity;
import com.achievement.domain.vo.PaperAuthorVO;
import com.achievement.mapper.PaperAuthorMapper;
import com.achievement.mappings.paper.PaperAuthorMapping;
import com.achievement.service.IPaperAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author CNKI
* @description 针对表【biz_paper_author(科研成果-学术论文-论文-论文作者)】的数据库操作Service实现
* @createDate 2025-02-13 15:49:32
*/
@Service
public class PaperAuthorServiceImpl extends ServiceImpl<PaperAuthorMapper, PaperAuthorEntity>
    implements IPaperAuthorService {

    @Autowired
    private PaperAuthorMapping mapping;


    /**
     * 删除作者
     * 根据论文 paperid，删除作者信息
     *
     * @param id id
     */
    @Override
    public boolean deleteByPaperId(String id) {

        //根据论文 paperid，删除作者信息
        LambdaQueryWrapper<PaperAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperAuthorEntity::getPaperId,id);

        return this.baseMapper.delete(queryWrapper)>0;
    }


    /**
     * 根据论文id，查询相关作者信息
     *
     * @param id id
     */
    @Override
    public List<PaperAuthorVO> getInfoByPaperId(String id) {

        LambdaQueryWrapper<PaperAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperAuthorEntity::getPaperId,id);
        queryWrapper.orderByAsc(PaperAuthorEntity::getOrderId);

        List<PaperAuthorEntity> list = this.list(queryWrapper);

        List<PaperAuthorVO> voList = list.stream().map(mapping::entity2Vo).collect(Collectors.toList());
        return voList;
    }

    /**
     * 新增作者
     *
     * @param dto dto
     */
    @Override
    public boolean add(List<PaperAuthorDTO> dto) {

        List<PaperAuthorEntity> entityList = dto.stream().map(mapping::dto2Entity).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(entityList)){
            return this.saveBatch(entityList);
        }

        return true;
    }

    /**
     * 删除作者
     * 根据论文 paperidList，删除作者信息
     *
     * @param ids List<String>
     */
    @Override
    public boolean deleteByPaperIdList(List<String> ids) {

        if (CollUtil.isEmpty(ids)){
            return true;
        }

        //根据论文 paperid，删除作者信息
        LambdaQueryWrapper<PaperAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PaperAuthorEntity::getPaperId,ids);

        return this.baseMapper.delete(queryWrapper)>0;

    }
}




